package hbnu.stockManage.util;

import java.util.Scanner;

//完成控制台输入工具类代码，封装从控制台（标准输入）读取用户输入的操作
public class ConsoleIo {
    private final Scanner scanner = new Scanner(System.in);

    public String readLine(String  prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }
    public int readInt(String prompt) {
         while ( true){
             System.out.print(prompt);
             String line = scanner.nextLine().trim();
             try{
                 return Integer.parseInt(line);
             }catch (NumberFormatException e){
                 System.out.println("输入错误，请重新输入");
             }
         }
    }

    public double readDouble(String prompt) {
        while ( true){
            System.out.print(prompt);
            String line = scanner.nextLine().trim();
            try{
                return Double.parseDouble(line);
            }catch (NumberFormatException e){
                System.out.println("输入错误，请重新输入");
            }
        }
    }
}
